<?php

$response = "";

function _cmdXML($aAttributes, $sData){
  $xml = "<cmd";
  foreach($aAttributes as $sAttribute => $sValue)
    $xml .= " $sAttribute=\"$sValue\"";
  if ($sData !== null && !@stristr($sData,'<![CDATA['))
    $xml .= "><![CDATA[$sData]]></cmd>";
  elseif ($sData !== null)
    $xml .= ">$sData</cmd>";
  else
    $xml .= "></cmd>";
  return $xml;
}

function addAssign($sTarget,$sAttribute,$sData) {
  global $response;
  $response .= _cmdXML(array("n"=>"as","t"=>$sTarget,"p"=>$sAttribute),$sData);
}

function addScript($sJS) {
  global $response;
  $response .= _cmdXML(array("n"=>"js"),$sJS);
}

function addScriptWithTimeout($sJS) {
  global $response;
  $response .= _cmdXML(array("n"=>"ls"),$sJS);
}

function addIncludeScript($sFileName) {
  global $response;
  $response .= _cmdXML(array("n"=>"in"),$sFileName);
}

function addIncludeCss($sFileName) {
  global $response;
  $response .= _cmdXML(array("n"=>"ic"),$sFileName);
}

function addAlert($sMsg){
  global $response;
  $response .= _cmdXML(array("n"=>"al"),$sMsg);
}

//function addScriptCall() {
//  $arguments = func_get_args();
//  $sFunc = array_shift($arguments);
//  $sData = $this->_buildObjXml($arguments);
//  $this->xml .= $this->_cmdXML(array("n"=>"jc","t"=>$sFunc),$sData);
//}

function xml2array($xmlargs){
  $ret = array();
  for ($i = 0 ; $i < count($xmlargs->e) ; $i++){
    $os = (string) $xmlargs->e[$i]->k;
    if(strlen($os) > 0)
      $ret[$os] = urldecode($xmlargs->e[$i]->v);
    else
      $ret[$i] = urldecode($xmlargs->e[$i]->v);
    unset($os);
  }
  return $ret;
}

function processResponse(){
  global $response;
  $ret = $response;
  $response = "";
  return $ret;
}
// procesa la petición llamando a la funcion que se ha solicitado,
// respecto a la raíz del proyecto.
@define('ZAJAXURI',@$_POST["zajaxUri"]);
include_once(ZAJAXURI);
$xmlargs = simplexml_load_string($_POST["zajaxArgs"]);
$zajaxargs = xml2array($xmlargs);
foreach ($zajaxargs as $key => $arg) {
	if (is_string($arg))
		$zajaxargs[$key] = str_replace('ZAJAXPLUS','+',$arg);
}
header("Content-type: text/xml; charset=utf-8");
print "<?xml version=\"1.0\" encoding=\"utf-8\" ?><xjx>"
. call_user_func(@$_POST["zajaxFcn"],$zajaxargs) . "</xjx>";
?>